package Harmonisation;

import java.util.Hashtable;

public class Note {
	private final String nom;
	private final int octave;
	private final int temps;
	private final int note;
	private final int midi;
	private final String lily;

	// Hashtable static qui contient les valeurs en int de chaques notes
	private static final Hashtable<String, Integer> valeurNote = new Hashtable<String,

	Integer>() {
		private static final long serialVersionUID = 6651792788948503640L;
		{
			// Premiere octave
			put("do1", 0);
			put("re1", 1);
			put("mi1", 2);
			put("fa1", 3);
			put("sol1", 4);
			put("la1", 5);
			put("si1", 6);
			// Deuxieme octave
			put("do2", 7);
			put("re2", 8);
			put("mi2", 9);
			put("fa2", 10);
			put("sol2", 11);
			put("la2", 12);
			put("si2", 13);
			// Troisieme octave
			put("do3", 14);
			put("re3", 15);
			put("mi3", 16);
			put("fa3", 17);
			put("sol3", 18);
			put("la3", 19);
			put("si3", 20);
			// Quatrieme octave
			put("do4", 21);
			put("re4", 22);
			put("mi4", 23);
			put("fa4", 24);
			put("sol4", 25);
			put("la4", 26);
			put("si4", 27);
		}
	};

	// Hashtable static qui contient les valeurs midi de chaques notes
	private static final Hashtable<String, Integer> valeurMidi = new Hashtable<String,

	Integer>() {
		private static final long serialVersionUID = 4313244813178900881L;
		{
			// Premiere octave
			put("do1", 24);
			put("re1", 26);
			put("mi1", 28);
			put("fa1", 29);
			put("sol1", 31);
			put("la1", 33);
			put("si1", 35);
			// Deuxieme octave
			put("do2", 36);
			put("re2", 38);
			put("mi2", 40);
			put("fa2", 41);
			put("sol2", 43);
			put("la2", 45);
			put("si2", 47);
			// Troisieme octave
			put("do3", 48);
			put("re3", 50);
			put("mi3", 52);
			put("fa3", 53);
			put("sol3", 55);
			put("la3", 57);
			put("si3", 59);
			// Quatrieme octave
			put("do4", 60);
			put("re4", 62);
			put("mi4", 64);
			put("fa4", 65);
			put("sol4", 67);
			put("la4", 69);
			put("si4", 71);
		}
	};

	// Hashtable static qui contient les valeurs midi de chaques notes
	private static final Hashtable<String, String> valeurLily = new Hashtable<String,

	String>() {
		private static final long serialVersionUID = 4313244813178900881L;
		{
			put("do", "c");
			put("re", "d");
			put("mi", "e");
			put("fa", "f");
			put("sol", "g");
			put("la", "a");
			put("si", "b");
		}
	};

	public Note(String note, int temps) {
		int t = note.length();
		this.nom = note.substring(0, t - 1);
		this.temps = temps;
		this.octave = Integer.parseInt(note.substring(t - 1, t));
		this.note = valeurNote.get(note);
		this.midi = valeurMidi.get(note);
		this.lily = valeurLily.get(nom);
	}

	public String getNom() {
		return nom;
	}

	public int getOctave() {
		return octave;
	}

	public int getNote() {
		return note;
	}

	public int getTemps() {
		return temps;
	}

	public int getMidi() {
		return midi;
	}

	public String getLily() {
		return lily;
	}

	public String getLily(Note notePrecedente, int octaveRelative) {
		String s = this.lily;
		int i;
		if (notePrecedente != null) {

			if (this.noteAuFormatNombre() > notePrecedente.noteAuFormatNombre()) {
				if (this.noteAuFormatNombre() - notePrecedente.noteAuFormatNombre() >= 4) {
					i = 0;
					while (i < ((((this.noteAuFormatNombre() - notePrecedente.noteAuFormatNombre() - 4) / 7) + 1))) {
						s += "'";
						i++;
					}
				}
			} else if (this.noteAuFormatNombre() < notePrecedente.noteAuFormatNombre()) {
				if (notePrecedente.noteAuFormatNombre() - this.noteAuFormatNombre() >= 4) {
					i = 0;
					while (i < (((notePrecedente.noteAuFormatNombre() - this.noteAuFormatNombre() - 4) / 7) + 1)) {
						s += ",";
						i++;
					}

				}
			}
		}

		if (this.temps == 2) {
			s += "2";
		} else if (this.temps == 3) {
			s += "2.";
		} else {
			s += "4";
		}

		return s;
	}

	@Override
	public String toString() {
		return "Note [nom=" + nom + ", octave=" + octave + ", temps=" + temps + ", note="

		+ note + ", midi=" + midi + "]";
	}

	public static String intEnNote(int i) {
		i = i % 7;
		String s = "";
		if (i == 0) {

			s = "do";

		} else if (i == 1) {
			s = "re";

		} else if (i == 2) {
			s = "mi";

		} else if (i == 3) {
			s = "fa";

		} else if (i == 4) {
			s = "sol";

		} else if (i == 5) {
			s = "la";

		} else if (i == 6) {
			s = "si";

		}

		return s;
	}

	public int nomEnInt() {

		if (this.nom.equals("do")) {
			return 0;
		} else if (this.nom.equals("re")) {
			return 1;
		} else if (this.nom.equals("mi")) {
			return 2;
		} else if (this.nom.equals("fa")) {
			return 3;
		} else if (this.nom.equals("sol")) {
			return 4;
		} else if (this.nom.equals("la")) {
			return 5;
		} else {
			return 6;
		}

	}

	public int noteAuFormatNombre() {
		return (this.octave - 1) * 7 + nomEnInt();
	}

	public int ecartEntreDeuxNote(Note n) {
		int i = this.noteAuFormatNombre() - n.noteAuFormatNombre();
		return i > 0 ? i : -i;
	}

	@Override
	public boolean equals(Object v) {
		if (v.getClass().equals(this.getClass())) {
			Note n = (Note) v;
			return this.nom.equals(n.getNom()) && this.note == n.getNote() && this.octave == n.getOctave();
		}

		return false;
	}

	public boolean equals(Note n) {
		return this.nom.equals(n.getNom()) && this.note == n.getNote() && this.octave == n.getOctave();
	}
}
